<?php
class CW_metaboxes {
   public function __construct() {
//	add_action( 'add_meta_boxes', array(&$this, 'init_background_image_box'));
//
//	add_action( 'save_post', array(&$this, 'save_landingpage_metainfo'), 1);
   }
   
   /**
    * Init metabox for background image on Passiv Indtægt Landingpage template
    */
   public function init_background_image_box()
   {
	add_meta_box( 
           'background_image_box',
           __( 'Landingpage baggrundsbillede', 'ant' ),
           array(&$this, 'display_background_image_box'),
           'page',
           'side',
           'high'
       );
   }
   
   /**
    * Display metabox for background image on Passiv Indtægt Landingpage template
    * 
    * @param WP_Post Object $post Current editing post object
    * @param array $meta_info Misc Meta Box info
    */
   public function display_background_image_box($post, $meta_info)
   {
	$landingpage_bg_image_url = get_post_meta($post->ID, 'landingpage_bg_image_url', true);
	$landingpage_bg_image_id = get_post_meta($post->ID, 'landingpage_bg_image_id', true);
	
	wp_nonce_field( plugin_basename( __FILE__ ), 'background_image_nonce' );
      ?>
      <div class="background_image_box passive_income_landingpage_metainfo">
         
	   <div class="line clearfix" style="padding: 5px 0;">
		Filnavn: <input type="text" name="landingpage_bg_image_url" id="landingpage_bg_image_url" value="<?php echo $landingpage_bg_image_url ?>" />
		<input type="hidden" name="landingpage_bg_image_id" id="landingpage_bg_image_id" value="<?php echo $landingpage_bg_image_id ?>" />
		<input type="button" value="Find billede" class="button landingpage_add_image" data-imagetype="bg" />
	   </div>
      </div>
      <?php
   }
   
   /**
    * Save meta info
    * 
    * @param int $post_id Current editing post id
    */
   public function save_landingpage_metainfo($post_id) 
   {
	/* Metainfo only for Passiv Indtægt Ebog landingpages */
	if($_POST['page_template'] === 'page_ant_landingpage.php')
	{
	   /**
	    * Save signup form content
	    */
	   if(isset($_POST['signup_form_nonce']))
	   {
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
		return;

		if ( !wp_verify_nonce( $_POST['signup_form_nonce'], plugin_basename( __FILE__ ) ) )
		return;

		if ( !current_user_can( 'edit_post', $post_id ) )
		return;

		$signup_form_headline = $_POST['signup_form_headline'];
		$signup_form_cta_text = $_POST['signup_form_cta_text'];
		$signup_form_html = $_POST['signup_form_html'];

		update_post_meta( $post_id, 'signup_form_headline', $signup_form_headline);
		update_post_meta( $post_id, 'signup_form_cta_text', $signup_form_cta_text);
		update_post_meta( $post_id, 'signup_form_html', $signup_form_html);
	   }
	   
	   /**
	    * Save signup form colors
	    */
	   if(isset($_POST['signup_form_color_nonce']))
	   {
//		dump($_POST);
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
		return;

		if ( !wp_verify_nonce( $_POST['signup_form_color_nonce'], plugin_basename( __FILE__ ) ) )
		return;

		if ( !current_user_can( 'edit_post', $post_id ) )
		return;

		$bg_color = $_POST['signup_bg_color'];
		
		$headline_color = $_POST['signup_headline_color'];
		
		$cta_text_color = $_POST['signup_cta_text_color'];
		
		$signup_input_field_font_color = $_POST['signup_input_field_font_color'];
		$signup_input_field_border_color = $_POST['signup_input_field_border_color'];
		$signup_input_field_border_focus_color = $_POST['signup_input_field_border_focus_color'];
		
		$btn_bg_color = $_POST['signup_btn_bg_color'];
		$btn_bg_hover_color = $_POST['signup_btn_bg_hover_color'];
		$btn_font_color = $_POST['signup_btn_font_color'];
		$btn_font_hover_color = $_POST['signup_btn_font_hover_color'];

		update_post_meta( $post_id, 'signup_bg_color', $bg_color);
		
		update_post_meta( $post_id, 'signup_headline_color', $headline_color);
		
		update_post_meta( $post_id, 'signup_cta_text_color', $cta_text_color);
		
		update_post_meta( $post_id, 'signup_input_field_font_color', $signup_input_field_font_color);
		update_post_meta( $post_id, 'signup_input_field_border_color', $signup_input_field_border_color);
		update_post_meta( $post_id, 'signup_input_field_border_focus_color', $signup_input_field_border_focus_color);

		update_post_meta( $post_id, 'signup_btn_bg_color', $btn_bg_color);
		update_post_meta( $post_id, 'signup_btn_bg_hover_color', $btn_bg_hover_color);
		update_post_meta( $post_id, 'signup_btn_font_color', $btn_font_color);
		update_post_meta( $post_id, 'signup_btn_font_hover_color', $btn_font_hover_color);
	   }

	   /**
	    * Save CTA image
	    */
	   if(isset($_POST['sub_headline_nonce']))
	   {
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
		return;

		if ( !wp_verify_nonce( $_POST['sub_headline_nonce'], plugin_basename( __FILE__ ) ) )
		return;

		if ( !current_user_can( 'edit_post', $post_id ) )
		return;

		$sub_headline = $_POST['sub_headline'];
		
		update_post_meta( $post_id, 'sub_headline', $sub_headline);
	   }

	   /**
	    * Save background image
	    */
	   if(isset($_POST['background_image_nonce']))
	   {
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
		return;

		if ( !wp_verify_nonce( $_POST['background_image_nonce'], plugin_basename( __FILE__ ) ) )
		return;

		if ( !current_user_can( 'edit_post', $post_id ) )
		return;

		$landingpage_bg_image_url = $_POST['landingpage_bg_image_url'];
		$landingpage_bg_image_id = $_POST['landingpage_bg_image_id'];

		update_post_meta( $post_id, 'landingpage_bg_image_url', $landingpage_bg_image_url);
		update_post_meta( $post_id, 'landingpage_bg_image_id', $landingpage_bg_image_id);
	   }
	}
   }
}

new CW_metaboxes();